<template>
	<view>
		<view class="head">
			<u-navbar leftIcon="arrow-leftward" leftText=" " :rightText="editing? '完成' : '編輯' " @rightClick="rightClick"
				:placeholder='true' title="報價車" @leftClick="leftClick">
			</u-navbar>
		</view>
		<view style="padding-bottom: 144rpx;">
			<view class="goods-detail" v-for="(item,index) in quotalist" :key="item.goods_id">
				<view class="detail-left">
					<view class="goods-left">
						<u-checkbox-group @change="selected(item)">
							<label>
								<u-checkbox activeColor="#645AED" shape="circle" class="selected" color="#555555"
									:checked="item.flag" /><text></text>
							</label>
						</u-checkbox-group>
						<view class="goods-img">
							<image @click="$store.dispatch('toDetails',item.goods_id)" :src="getFirstImageLink(item.goods_images)" mode="aspectFit"></image>
							<!-- <image :src="item.goodsImage" style="width: 150rpx;height: 140rpx;"></image> -->
						</view>
					</view>
					<view class="size">
						<view class="goods-title" @click="$store.dispatch('toDetails',item.goods_id)">{{item.goods_name}}</view>
						<view class="center">
							{{item.attr_value}}
						</view>
						<view class="bottom">
							<text class="goods-price">MOP {{item.goods_price}}</text>
							<view class="detail-right">
								<view class="stepper">
									<view class="stepper-btn" @click="reduce(item)">
										<image src="@/static/images/quantity_reduce.png" mode=""></image>
									</view>
                  <u--input style="width: 100rpx;" inputAlign="center"  border="none" v-model="item.goods_number" fontSize="26rpx"  @change="leavinput(item)" @blur="leavinput(item)"
                            type="number">
                  </u--input>
<!--									<view class="stepper-value">{{ item.goods_number }}</view>-->
									<view class="stepper-btn" @click="add(item)">
										<image src="@/static/images/quantity_add.png" mode=""></image>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="address" v-if="quotalist.length>0">
				<view class="a-l">
					備註
				</view>
				<view class="a-input">
					<input v-model="keyword" type="text" placeholder="50漢字內" name="" id="">
				</view>
			</view>
		</view>
		<view class="end">
			<view class="end-left">
				<u-checkbox-group @change="selectgoods()">
					<label style="display: flex;align-items: center;">
						<u-checkbox activeColor="#645AED" shape="circle" :checked="allchecked" />全選
					</label>
				</u-checkbox-group>

			</view>
			<view v-if="!editing" class="end-right">
				<view class="end-price">
					<!-- 	<view>
						總計：<text style="font-size: 30rpx;font-weight: bold;color: #4949C3;">MOP {{totalPrices}}</text>
					</view> -->
					<view style="font-size: 22rpx;font-weight: 500;color: #333333;">
						共{{totalNums}}件
					</view>
				</view>
				<view class="btn" @click="Tosubmit">
					提交
				</view>
<!--				<view class="btn" @click="addcart">-->
<!--					加入購物車-->
<!--				</view>-->

			</view>
			<view v-else class="end-right">
				<view class="btn red" @click="delegood">
					取消報價
				</view>
			</view>
		</view>
	</view>

</template>

<script>
	import {
		mapActions,
		mapState
	} from 'vuex'
	import {
		getquote_list,
		delquote,
		addquoteorder,
		addquotecart,
		changequotegoods
	} from '@/network/api/user.js'
	export default {
		data() {
			return {
				keyword: "",
				editing: false,
				quotalist: [],
				allchecked: false,
				totalNums: 0,
			}
		},
		onShow() {
			this.getQuoteCart()
		},
		computed: {

		},
		methods: {
      leavinput(item) {
        console.log(item)
        if (item.goods_number < 1) {
          item.goods_number = 1
          return uni.showToast({
            title: '最少不能低于一件商品',
            icon: 'none'
          })
        }
        let data = {
          quo_id: item.quo_id,
          goods_number: item.goods_number,
          goods_attr_id: item.goods_attr_id || '',
          goods_specification_id: item.goods_specification_id || ''
        }
        this.quotachange(data)
      },
			isAnyItemSelected() {
				return this.quotalist.some(item => item.flag);
			},
			leftClick() {
				uni.navigateBack({
					delta: 1
				})
			},
			//取消报价
			async delegood() {
				if (!this.isAnyItemSelected()) {
					uni.showToast({
						title: "请至少选择一项商品",
						icon: "none"
					});
					return;
				}
				let toDeleteList = [];
				let toKeepList = [];
				this.quotalist.forEach(item => {
					if (item.flag) {
						// 商品被选择，加入要删除的列表
						toDeleteList.push(item);
					} else {
						// 商品未被选择，加入不要删除的列表
						toKeepList.push(item);
					}
				});
				this.quotalist = toKeepList
				let quo_id = toDeleteList.map(item => item.quo_id);
				let data = {
					quo_id: quo_id
				}
				const res = await delquote(data)
				if (res.data.code == 1) {
					uni.showToast({
						title: "取消报价成功",
						icon: "none"
					});
					this.getQuoteCart()
				}
			},
			//加入购物车
			async addcart() {
				if (!this.isAnyItemSelected()) {
					uni.showToast({
						title: "请至少选择一项商品",
						icon: "none"
					});
					return;
				}
				let toDeleteList = [];
				this.quotalist.forEach(item => {
					if (item.flag) {
						// 商品被选择，加入要删除的列表
						toDeleteList.push(item);
					}
				});
				console.log(toDeleteList);
				let data={
					goods:toDeleteList
				}
				const res = await addquotecart(data)
				if(res.data.code==1){
					uni.showToast({
						title: "添加购物车订单成功",
						icon: "none"
					});
				}else if(res.data.code==2){
					uni.showToast({
						title: "请勿重复下单",
						icon: "none"
					});
				}else if(res.data.code==5){
          uni.showToast({
            title: "商品中存在非公司商品，请联系管理员",
            icon: "none"
          });
        }
			},
			//提交
			async Tosubmit() {
				if (!this.isAnyItemSelected()) {
					uni.showToast({
						title: "请至少选择一项商品",
						icon: "none"
					});
					return;
				}
				let toDeleteList = [];
				let toKeepList = [];
				this.quotalist.forEach(item => {
					if (item.flag) {
						// 商品被选择，加入要删除的列表
						toDeleteList.push(item);
					}
				});
				let quo_id = toDeleteList.map(item => item.quo_id);
				let data = {
					postscript: this.keyword,
					goods: quo_id
				}
				uni.showLoading({
					title: "提交中，请稍后...",
					mask: true,
				});
				const res = await addquoteorder(data)
				this.keyword = ''
				if (res.data.code == 1) {
					uni.hideLoading()
					uni.showToast({
						title: "添加報價車訂單成功",
						icon: "none"
					});
					this.getQuoteCart()
				} else {
					uni.hideLoading()
				}
			},
			selectgoods() {
				this.allchecked = !this.allchecked
				if (this.allchecked) {
					this.quotalist.forEach(item => {
						// this.checked = true
						item.flag = true
					})
				} else {
					this.quotalist.forEach(item => {
						// this.checked = false
						item.flag = false
					})
				}
				this.totalNum()
			},
			selected(item) {
				console.log(item);
				item.flag = !item.flag
				this.allchecked = this.quotalist.every(item => item.flag);
				this.totalNum()
			},
			reduce(item) {
				let num = item.goods_number
				if (num > 1) {
					num -= 1
				} else if (num = 1) {
					uni.showToast({
						title: "该宝贝不能减少了哟~"
					})
				}
				item.goods_number = num
				let data = {
					quo_id: item.quo_id,
					goods_number: item.goods_number,
					goods_attr_id: item.goods_attr_id || '',
					goods_specification_id: item.goods_specification_id || ''
				}
				this.totalNum()
				this.quotachange(data)
			},
			async quotachange(data) {
				const res = await changequotegoods(data)
        if (res.data.code == 2) {
          this.getQuoteCart()
          uni.showToast({
            title: "商品庫存不足，請聯繫管理員",
            icon: "none"
          });
        }
			},
			add(item) {
				console.log(item);
				let num = item.goods_number
				item.goods_number = num + 1
				let data = {
					quo_id: item.quo_id,
					goods_number: item.goods_number,
					goods_attr_id: item.goods_attr_id || '',
					goods_specification_id: item.goods_specification_id || ''
				}
				this.totalNum()
				this.quotachange(data)
			},
			totalNum() {
				this.totalNums = 0;
				this.quotalist.map(item => {
					item.flag ? this.totalNums += item.goods_number : this.totalNums += 0
				})
			},
			async getQuoteCart() {
				const res = await getquote_list()
				console.log(res);
				this.quotalist = res.data.quote
				this.quotalist.map((item) => {
					item.flag = false;
				});
				console.log(this.quotalist, 'qwe');
			},
			rightClick() {
				this.editing = !this.editing;
				this.totalNum()
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #f5f6f9;
	}

	.address {
		background-color: #fff;
		padding: 32rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1rpx solid #DEDEDE;

		.a-input {
			display: flex;
			justify-content: flex-end;
		}

		.a-l {
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #333333;

			.top {
				.name {
					font-size: 28rpx;
					font-weight: bold;
					color: #333333;
					margin-right: 24rpx;
				}

				.phone {
					font-size: 28rpx;
					font-weight: bold;
					color: #333333;
				}
			}

			.bottom {
				font-size: 28rpx;
				font-weight: 500;
				color: #333333;
			}
		}

		.a-r {}
	}

	.end {
		width: 100%;
		height: 110rpx;
		background-color: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		display: flex;
		align-items: center;
		justify-content: space-between;

		&-left {
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #333333;
			display: flex;
			justify-content: space-between;
			padding: 0 30rpx;

			.end-flex {
				display: flex;
				align-items: center;
			}
		}

		&-right {
			margin-right: 32rpx;
			display: flex;
			align-items: center;

			.end-price {
				font-size: 28px;
				font-family: PingFang SC;
				font-weight: 500;

			}

			.btn {
				margin-left: 20rpx;
				text-align: center;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: bold;
				line-height: 0rpx;
				color: #FFFFFF;
				width: 170rpx;
				height: 80rpx;
				line-height: 80rpx;
				background: #645AED;
				border-radius: 30rpx;
			}

			.red {
				background: #ED0000;
			}

		}
	}

	.goods {
		line-height: 80rpx;
		background-color: #FFFFFF;

		&-detail {
			// display: flex;
			padding: 32rpx;
			// margin: 0 32rpx;
			background-color: #fff;
			justify-content: space-between;
			border-bottom: 1rpx solid #F1F1F1;
			align-items: center;

			.detail-left {
				display: flex;

				.goods-left {
					display: flex;
					align-items: center;

					.goods-img {
						margin-left: 24rpx;
						width: 180rpx;
						height: 180rpx;
						background: #FFFFFF;
						border: 1rpx solid #DEDEDE;
						border-radius: 30rpx;
						display: flex;
						justify-content: center;
						align-items: center;

						image {
						   max-width: 150rpx;
						   max-height: 150rpx;
						}
					}
				}

				.size {
					flex: 999;
					display: flex;

					justify-content: space-between;
					flex-direction: column;
					margin-left: 30rpx;

					.center {
						font-size: 22rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
						margin-top: 8rpx;
					}

					.bottom {
						margin-top: 22rpx;
						display: flex;
						align-items: center;
						justify-content: space-between;
					}

					.goods-title {
						font-size: 28rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #333333;
					}

					.goods-price {
						font-size: 28rpx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #4949C3;
					}
				}

			}



			.detail-right {
				.stepper {
					display: flex;
					// align-items: center;
					vertical-align: middle;

					.stepper-value {
						margin: 0 20rpx;
						font-size: 24rpx;
						line-height: 43rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #333333;
					}

					.stepper-btn {
						image {
							width: 44rpx;
							height: 44rpx;
						}
					}
				}

				text {
					width: 50rpx;
					line-height: 50rpx;
					text-align: center;
					display: inline-block;
					background-color: #F7F7F7;
					margin-right: 10rpx;
				}

				.add {
					color: #FA4305;
					border-radius: 10rpx 30rpx 30rpx 10rpx;
					margin-right: 20rpx;
				}

				.subtract {
					border-radius: 30rpx 10rpx 10rpx 30rpx;
				}
			}
		}
	}
</style>
